<?php
require_once "lib/nusoap.php";

//	Pass remote desktop: S@ngeL1

// $hostname="1.234.91.160";					// Server Korea
$hostname="112.213.84.141";					// Server Vietnam
$username="cam";
$password="5678";
$databasename="shopping";

// $api_key = "AIzaSyAVLc8QahfI3F3M3B87IfIMBCB9YdssoFI";
$api_key = "AIzaSyC1tpZ_RPGgo9qtmCalRvLGO72hH9ozt-4";

date_default_timezone_set("Asia/Saigon");

class Notify {
	public $id;
	public $id_mem;
	public $id_sender;
	public $uid_sender;
	public $name_sender;
	public $id_owner;
	public $uid_owner;
	public $name_owner;
	public $content;
	public $id_post;
	public $image;
	public $type;
	public $cm_type;
	public $unread;
	public $date;
	public $status;
	
	public function __construct($id, $id_mem, $id_sender, $uid_sender, $name_sender, $id_owner, $uid_owner, $name_owner, $content, $id_post, $image, $type, $cm_type, $unread, $date, $status) {
		$this->id = $id;
		$this->id_mem = $id_mem;
		$this->id_sender = $id_sender;
		$this->uid_sender = $uid_sender;
		$this->name_sender = $name_sender;
		$this->id_owner = $id_owner;
		$this->uid_owner = $uid_owner;
		$this->name_owner = $name_owner;
		$this->content = $content;
		$this->id_post = $id_post;
		$this->image = $image;
		$this->type = $type;
		$this->cm_type = $cm_type;
		$this->unread = $unread;
		$this->date = $date;
		$this->status = $status;
	}
}

function GetBestOfDay($day, $idaccount) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idaccount = urldecode($idaccount);
	$day = urldecode($day);
	
	
	$curDay= date("Y-m-d");
	//$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));
	$NewDate = date('Y-m-d', strtotime($day));
	
	// $str = "SET @num := 0, @idc := -1;";
	// mysql_query($str);
	
	// $str = "SELECT		p.*, 	IFNULL((	SELECT	SUM(l.`like`)
											// FROM	`like` l
											// WHERE	l.`id_account`='$idaccount' AND l.`id_product`=p.`id` AND l.`type`=1
										// ), 0) AS `liked`,
								// @num := IF(@idc = p.`id_category`, @num + 1, 1) AS `row_number`,
								// @idc := p.`id_category` AS `dummy`,
								// c.`nameEN`	AS	`category_name_en`,
								// c.`nameKR`	AS	`category_name_kr`,
								// c.`nameVN`	AS	`category_name_vn`,
								// c.`color`	AS	`color`,
								// c1.`status`	AS	`st_parent`
			// FROM		(	SELECT		p1.*,
										// p1.`priceVN` AS `price_vn`
							// FROM		`product` p1, `category` c2
							// WHERE		( IF 
											// (`end_date` IS NULL, 
											// TRUE, 
											// ('$day' BETWEEN `date` AND `end_date`)))
										// AND	`quantity`=1
										// AND c2.`id`=p1.`id_category`
							// ORDER BY	`id_category`		ASC,
										// `price_vn`<=500000	DESC,
										// `count`				DESC,
										// `create_day`		DESC,
										// `likes`				DESC) p
			// INNER JOIN	`category` c
			// ON			c.`id`=p.`id_category`
			// INNER JOIN	`category` c1
			// ON			c.`id_parent`=c1.`id`
			// HAVING		`row_number`<=4		AND
						// (`price_vn`<=500000 OR `best_today`=1) 	AND
						// `st_parent`=1
			// ORDER BY	p.`best_today`=1	DESC,
						// c.`pos`				ASC,
						// c.`pos1`			ASC;";
						
	$str = "SELECT		p.*, 	IFNULL((	SELECT	SUM(l.`like`)
											FROM	`like` l
											WHERE	l.`id_account`='$idaccount' AND l.`id_product`=p.`id` AND l.`type`=1
										), 0) AS `liked`,
								c.`nameEN`	AS	`category_name_en`,
								c.`nameKR`	AS	`category_name_kr`,
								c.`nameVN`	AS	`category_name_vn`,
								c.`color`	AS	`color`,
								p.`priceVN`	AS	`price_vn`
			FROM		`product` p
			INNER JOIN	`category` c
			ON			c.`id`=p.`id_category`
			WHERE		c.`status`=1	AND
						( IF 
							(`end_date` IS NULL, 
							TRUE, 
							('$day' BETWEEN `date` AND `end_date`)))	AND
						p.`quantity`=1		AND
						p.`top_seller`=1	AND
						c.`id_trademarks`=0
			ORDER BY	p.`create_day` 						DESC,
						p.`priceVN` * (100-p.`sale_off`)	ASC,
						p.`date` 							DESC;";
	  // INNER JOIN	`category` c1
			// ON			c.`id_parent`=c1.`id`
			// WHERE		c1.`status`=1	AND
	$result=mysql_query($str);
	$rows = array();
	
	while($r = mysql_fetch_assoc($result)) {
		// $r['price_vn'] = (string) (round( ((float) $r['price_vn']) / 10000) * 10000);
		// $rows[] = $r;
		$rows[] = calculateSaleOff($r);
	}
	
	mysql_close($conn);
	return json_encode($rows);
}

function GetInfoMemberByEmail( $email, $myid ) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$email = urldecode($email);
	$myid  = urldecode($myid);
	
	$sql = "SELECT		a.*, IFNULL((	SELECT	COUNT(*)
											FROM	`follower` f
											WHERE	f.`id_follow`=a.`id` AND
													f.`id_mem`=$myid
										), 0) AS `followed`
			FROM 		`account` a
			WHERE  		`email` = '$email';";
	
	$result1 = mysql_query($sql);
	$result2 = new stdClass();
	if (mysql_num_rows($result1)>0)
		$result2 = mysql_fetch_assoc($result1);
		
	mysql_close($conn);
	return json_encode($result2);
}

function CheckEmail($email) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$email=urldecode($email);
	$result=mysql_query("SELECT * FROM `account` a WHERE a.`email`='$email';");
	$result2=mysql_num_rows($result);
	mysql_close($conn);
	if ($result2>0)
		return 1;
	return 0;
}

function CheckLogin($email,$pword) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$email = urldecode($email);
	$pword = urldecode($pword);
	
	$str1 = "SELECT		*
			 FROM		`account` a
			 WHERE		a.`email`='$email'";
			 
	$result1 = mysql_query($str1);
	if (mysql_num_rows($result1)<=0) {
		mysql_close($conn);
		return -1;
	}
	
	$result2 = mysql_fetch_assoc($result1);
	mysql_close($conn);
	
	if ($result2['password']!=$pword)
		return 0;
	return $result2['id'];
}


function Signup($name, $email, $pword) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$name  = urldecode($name);
	$email = urldecode($email);
	$pword = urldecode($pword);
	
	$str1 = "SELECT		*
			 FROM		`account` a
			 WHERE		a.`email`='$email'";
	
	$result1 = mysql_query($str1);
	if (mysql_num_rows($result1)>0) {
		mysql_close($conn);
		return -1;
	}
	
	$date = date("Y-m-d H:i:s");
	$str2 = "INSERT INTO `account`	(`name`, `email`, `password`, `image`, `profile`, `area`, `join_day`, `status`, `web`)
			 VALUES					('$name', '$email', '$pword', '', '', '', '$date', 1, 0)";
			 
	$result2 = mysql_query($str2);
	$result3 = mysql_insert_id();
	
	mysql_close($conn);
	return $result3;
}



function Signup1($name, $email, $pword, $gcmid) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$name  = urldecode($name);
	$email = urldecode($email);
	$pword = urldecode($pword);
	$gcmid = urldecode($gcmid);
	
	$str1 = "SELECT		*
			 FROM		`account` a
			 WHERE		a.`email`='$email'";
	
	$result1 = mysql_query($str1);
	if (mysql_num_rows($result1)>0) {
		mysql_close($conn);
		return -1;
	}
	
	$date = date("Y-m-d H:i:s");
	$str2 = "INSERT INTO `account`	(`name`, `email`, `password`, `image`, `profile`, `area`, `join_day`, `status`, `web`, `gcm_id`)
			 VALUES					('$name', '$email', '$pword', '', '', '', '$date', 1, 0, '$gcmid')";
			 
	$result2 = mysql_query($str2);
	$result3 = mysql_insert_id();
	
	mysql_close($conn);
	return $result3;
}


function GetProductDetail($idproduct) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idproduct = urldecode($idproduct);
	
	$str = "SELECT 		p.*
			FROM 		`product_detail` p
			WHERE 		p.`id_product`='$idproduct'
			ORDER BY	p.`pos`	ASC;";
			
	$result = mysql_query($str);
	$rows = array();
	
	while ($r = mysql_fetch_assoc($result))
		$rows[] = $r;
	
	mysql_close($conn);
	return json_encode($rows);
}


function GetCommentOfProduct($idproduct, $type) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idproduct = urldecode($idproduct);
	$type = urldecode($type);
	
	$str = "SELECT 		c.*, a.`image`, a.`name`, a.`nick_name`
			FROM 		`comment` c
			INNER JOIN 	`account` a
			ON			a.`id`=c.`id_account`
			WHERE 		c.`id_product`='$idproduct' AND c.`status`=1 AND c.`type`='$type'
			ORDER BY	c.`date`	ASC;";
	
	$result = mysql_query($str);
	$rows = array();
	
	while ($r = mysql_fetch_assoc($result))
		$rows[] = $r;
	
	mysql_close($conn);
	return json_encode($rows);;
}



function NewComment($idproduct, $idaccount, $content, $type) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idproduct = urldecode($idproduct);
	$idaccount = urldecode($idaccount);
	$content   = urldecode($content);
	$type      = urldecode($type);
	
	$date = date("Y-m-d H:i:s");
	$str2 = "INSERT INTO `comment` 	(`id_account`, `id_product`, `content`, `date`, `type`, `status`)
			 VALUES 				('$idaccount', '$idproduct', '$content', '$date', '$type', 1)";
	$result2 = mysql_query($str2);
	$result3 = mysql_insert_id();
	mysql_close($conn);
	
	if (!$result2)
		return -1;
	return $result3;
}



function NewComment1($idproduct, $idaccount, $content, $type) {
	// global $hostname;
	// global $username;
	// global $password;
	// global $databasename;
	
	// $idproduct = urldecode($idproduct);
	// $idaccount = urldecode($idaccount);
	// $content   = urldecode($content);
	// $type      = urldecode($type);
	
	// $dbo = new PDO("mysql:host=$hostname;dbname=$databasename", "$username", "$password");
	// $dbo->exec("SET NAMES 'utf8'");
	
	// $date = date("Y-m-d H:i:s");
	// $str2 = "INSERT INTO `comment` 	(`id_account`, `id_product`, `content`, `date`, `type`, `status`)
			 // VALUES 				('$idaccount', '$idproduct', '$content', '$date', '$type', 1)";
	// $result3 = -1;
	
	// try {
		// $dbo->beginTransaction();
		// $result2 = $dbo->exec($str2);
		// $result3 = $dbo->lastInsertId();
		// $dbo->commit();
	// } catch (PDOException $ex) {
		// $dbo->rollBack();
		// $dbo = null;
		// return -1;
	// }
	// $dbo = null;
	// return $result3;
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idproduct = urldecode($idproduct);
	$idaccount = urldecode($idaccount);
	$content   = urldecode($content);
	$type      = urldecode($type);
	
	$date = date("Y-m-d H:i:s");
	$str2 = "INSERT INTO `comment` 	(`id_account`, `id_product`, `content`, `date`, `type`, `status`)
			 VALUES 				('$idaccount', '$idproduct', '$content', '$date', '$type', 1)";
	$result2 = mysql_query($str2);
	$result3 = mysql_insert_id();
	mysql_close($conn);
	
	if (!$result2)
		return -1;
	return $result3;
}



function NewLike($idproduct, $idaccount, $liked, $type) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idproduct = urldecode($idproduct);
	$idaccount = urldecode($idaccount);
	$liked     = urldecode($liked);
	$type      = urldecode($type);
	
	$str1 = "SELECT	*
			 FROM	`like` l
			 WHERE	l.`id_account`=$idaccount	AND
					l.`id_product`=$idproduct	AND
					l.`type`=$type;";
	$result1 = mysql_query($str1);
	$result6 = mysql_num_rows($result1);
	if (($result6>0 && $liked==1) || ($result6<=0 && $liked==-1)) {
		mysql_close($conn);
		return -1;
	}
	
	$str2 = "";
	$date = date("Y-m-d H:i:s");
	if ($liked==1)
		$str2 = "INSERT INTO `like`	(`id_account`, `id_product`, `like`, `datetime`, `type`)
				 VALUES				('$idaccount', '$idproduct', '$liked', '$date', '$type');";
	if ($liked==-1)
		$str2 = "DELETE FROM 	`like`
				 WHERE			`id_account`=$idaccount	AND
								`id_product`=$idproduct	AND
								`type`=$type;";
	$result2 = mysql_query($str2);
	if (!$result2) {
		mysql_close($conn);
		return -1;
	}
	
	$str3 = "";
	if ($type==1)
		$str3 = "SELECT 	p.`likes`
				 FROM 		`product` p
				 WHERE 		p.`id`='$idproduct'";
	if ($type==2)
		$str3 = "SELECT 	p.`likes`
				 FROM 		`post` p
				 WHERE 		p.`id`='$idproduct'";
	$result3 = mysql_query($str3);
	$result4 = mysql_fetch_assoc($result3);
	$result5 = $result4['likes'];
	mysql_close($conn);
	
	return $result5;
}



function NewOrder($idaccount, $address, $city, $state, $phone, $payment, $shipping, $shippingfee, $memo, $detail) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idaccount 		= urldecode($idaccount);
	$address   		= urldecode($address);
	$city      		= urldecode($city);
	$state     		= urldecode($state);
	$phone     		= urldecode($phone);
	$payment   		= urldecode($payment);
	$shipping  		= urldecode($shipping);
	$detail    		= urldecode($detail);
	$memo    		= urldecode($memo);
	$shippingfee	= urldecode($shippingfee);
	
	$details = json_decode($detail, TRUE);
	
	$str1 = "SELECT	`name`
			 FROM	`rate`
			 WHERE	`id`=4;";
	$result7 = mysql_query($str1);
	$result8 = mysql_fetch_assoc($result7);
	$result9 = $result8['name'];
	
	$date = date("Y-m-d H:i:s");
	$str1 = "INSERT INTO `order`	(`code_order`, `id_account`, `total`, `totalVN`, `address`, `city`, `state`, `phone`, `payment`, `ship`, `memo`, `date`, `status`)
			 VALUES					('OD_APP_$result9', '$idaccount', 0, 0, '$address', '$city', '$state', '$phone', '$payment', '$shippingfee', '$memo', '$date', 0);";
	$result1 = mysql_query($str1);
	$result4 = mysql_insert_id();
	
	if (!$result1) { 
		mysql_close($conn);
		return -1;
	}
	
	foreach ($details as $od) {
		$idorder	= $od['id_order'];
		$idproduct	= $od['id_product'];
		$color		= $od['color'];
		$size		= $od['size'];
		$price		= $od['price'];
		$pricevn	= $od['priceVN'];
		$commission = $od['commission'];
		$quatily	= $od['quantity'];
		
		$str2 = "INSERT INTO 	`order_detail`	(`id_order`, `id_product`, `color`, `size`, `price`, `priceVN`, `commission`, `quantity`)
				 VALUES							('$result4', '$idproduct', '$color', '$size', '$price', '$pricevn', 0, '$quatily');";
				 
		$result2 = mysql_query($str2);
		if (!$result2) {
			mysql_close($conn);
			return -1;
		}
	}
	
	mysql_close($conn);
	
	return $result4;
}



function NewOrderNew($idaccount, $address, $city, $state, $phone, $payment, $shipping, $shippingfee, $memo, $detail, $email, $name) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idaccount 		= urldecode($idaccount);
	$address   		= urldecode($address);
	$city      		= urldecode($city);
	$state     		= urldecode($state);
	$phone     		= urldecode($phone);
	$payment   		= urldecode($payment);
	$shipping  		= urldecode($shipping);
	$detail    		= urldecode($detail);
	$memo    		= urldecode($memo);
	$email    		= urldecode($email);
	$name    		= urldecode($name);
	$shippingfee	= urldecode($shippingfee);
	
	$details = json_decode($detail, TRUE);
	
	$str1 = "SELECT	`name`
			 FROM	`rate`
			 WHERE	`id`=4;";
	$result7 = mysql_query($str1);
	$result8 = mysql_fetch_assoc($result7);
	$result9 = $result8['name'];
	
	$date = date("Y-m-d H:i:s");
	$str1 = "INSERT INTO `order`	(`code_order`, `id_account`, `total`, `totalVN`, `address`, `city`, `state`, `phone`, `payment`, `ship`, `memo`, `date`, `status`, `email`, `name`)
			 VALUES					('OD_APP_$result9', '$idaccount', 0, 0, '$address', '$city', '$state', '$phone', '$payment', '$shippingfee', '$memo', '$date', 0, '$email', '$name');";
	$result1 = mysql_query($str1);
	$result4 = mysql_insert_id();
	
	if (!$result1) { 
		mysql_close($conn);
		return -1;
	}
	
	foreach ($details as $od) {
		$idorder	= $od['id_order'];
		$idproduct	= $od['id_product'];
		$color		= $od['color'];
		$size		= $od['size'];
		$price		= $od['price'];
		$pricevn	= $od['priceVN'];
		$commission = $od['commission'];
		$quatily	= $od['quantity'];
		
		$str2 = "INSERT INTO 	`order_detail`	(`id_order`, `id_product`, `color`, `size`, `price`, `priceVN`, `commission`, `quantity`)
				 VALUES							('$result4', '$idproduct', '$color', '$size', '$price', '$pricevn', 0, '$quatily');";
				 
		$result2 = mysql_query($str2);
		if (!$result2) {
			mysql_close($conn);
			return -1;
		}
	}
	
	mysql_close($conn);
	
	return $result4;
}



function NewOrder1($idaccount, $address, $city, $state, $phone, $payment, $shipping, $shippingfee, $memo, $detail, $email, $name) {
	// global $hostname;
	// global $username;
	// global $password;
	// global $databasename;
	
	// $dbo = new PDO("mysql:host=$hostname;dbname=$databasename", "$username", "$password");
	// $dbo->exec("SET NAMES 'utf8'");
	// $idaccount 		= urldecode($idaccount);
	// $address   		= urldecode($address);
	// $city      		= urldecode($city);
	// $state     		= urldecode($state);
	// $phone     		= urldecode($phone);
	// $payment   		= urldecode($payment);
	// $shipping  		= urldecode($shipping);
	// $detail    		= urldecode($detail);
	// $memo    		= urldecode($memo);
	// $email    		= urldecode($email);
	// $name    		= urldecode($name);
	// $shippingfee	= urldecode($shippingfee);
	
	// $details = json_decode($detail, TRUE);
	// $result9 = "";
	// try {
		// $str1 = "SELECT	`name`
				 // FROM	`rate`
				 // WHERE	`id`=4;";
		// $result7 = $dbo->query($str1);
		// $result8 = $result7->fetchAll(PDO::FETCH_ASSOC);
		// $result9 = $result8[0]['name'];
	// } catch (PDOException $ex) {
		// $dbo = null;
		// return -1;
	// }
	
	// $result4 = -1;
	// try {
		// $dbo->beginTransaction();
		// $date = date("Y-m-d H:i:s");
		// $str1 = "INSERT INTO `order`	(`code_order`, `id_account`, `total`, `totalVN`, `address`, `city`, `state`, `phone`, `payment`, `ship`, `memo`, `date`, `status`, `email`, `name`)
				 // VALUES					('OD_APP_$result9', '$idaccount', 0, 0, '$address', '$city', '$state', '$phone', '$payment', '$shippingfee', '$memo', '$date', 0, '$email', '$name');";
		// $result1 = $dbo->exec($str1);
		// $result4 = $dbo->lastInsertId();
	
		// foreach ($details as $od) {
			// $idorder	= $od['id_order'];
			// $idproduct	= $od['id_product'];
			// $color		= $od['color'];
			// $size		= $od['size'];
			// $price		= $od['price'];
			// $pricevn	= $od['priceVN'];
			// $commission = $od['commission'];
			// $quatily	= $od['quantity'];
			
			// $str2 = "INSERT INTO 	`order_detail`	(`id_order`, `id_product`, `color`, `size`, `price`, `priceVN`, `commission`, `quantity`)
					 // VALUES							('$result4', '$idproduct', '$color', '$size', '$price', '$pricevn', 0, '$quatily');";
					 
			// $result2 = $dbo->exec($str2);
		// }
		// $dbo->commit();
	// } catch (PDOException $ex) {
		// $dbo->rollBack();
		// $dbo = null;
		// return -1;
	// }
	
	// $dbo = null;
	// return $result4;
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idaccount 		= urldecode($idaccount);
	$address   		= urldecode($address);
	$city      		= urldecode($city);
	$state     		= urldecode($state);
	$phone     		= urldecode($phone);
	$payment   		= urldecode($payment);
	$shipping  		= urldecode($shipping);
	$detail    		= urldecode($detail);
	$memo    		= urldecode($memo);
	$email    		= urldecode($email);
	$name    		= urldecode($name);
	$shippingfee	= urldecode($shippingfee);
	
	$details = json_decode($detail, TRUE);
	
	$str1 = "SELECT	`name`
			 FROM	`rate`
			 WHERE	`id`=4;";
	$result7 = mysql_query($str1);
	$result8 = mysql_fetch_assoc($result7);
	$result9 = $result8['name'];
	
	$date = date("Y-m-d H:i:s");
	$str1 = "INSERT INTO `order`	(`code_order`, `id_account`, `total`, `totalVN`, `address`, `city`, `state`, `phone`, `payment`, `ship`, `memo`, `date`, `status`, `email`, `name`)
			 VALUES					('OD_APP_$result9', '$idaccount', 0, 0, '$address', '$city', '$state', '$phone', '$payment', '$shippingfee', '$memo', '$date', 0, '$email', '$name');";
	$result1 = mysql_query($str1);
	$result4 = mysql_insert_id();
	
	if (!$result1) { 
		mysql_close($conn);
		return -1;
	}
	
	foreach ($details as $od) {
		$idorder	= $od['id_order'];
		$idproduct	= $od['id_product'];
		$color		= $od['color'];
		$size		= $od['size'];
		$price		= $od['price'];
		$pricevn	= $od['priceVN'];
		$commission = $od['commission'];
		$quatily	= $od['quantity'];
		
		$str2 = "INSERT INTO 	`order_detail`	(`id_order`, `id_product`, `color`, `size`, `price`, `priceVN`, `commission`, `quantity`)
				 VALUES							('$result4', '$idproduct', '$color', '$size', '$price', '$pricevn', 0, '$quatily');";
				 
		$result2 = mysql_query($str2);
		if (!$result2) {
			mysql_close($conn);
			return -1;
		}
	}
	
	mysql_close($conn);
	
	return $result4;
}



function NewOrderDetail($order, $product, $color, $size, $price, $quantity) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idorder	= urldecode($order);
	$idproduct 	= urldecode($product);
	$color      = urldecode($color);
	$size     	= urldecode($size);
	$price      = urldecode($price);
	$quantity   = urldecode($quantity);
	
	$str1 = "INSERT INTO 	`order_detail`	(`id_order`, `id_product`, `color`, `size`, `price`, `commission`, `quatily`)
			 VALUES							('$idorder', '$idproduct', '$color', '$size', '$price', 0, '$quantity')";
	$result1 = mysql_query($str1);
	$result3 = mysql_insert_id();
	mysql_close($conn);
	
	if (!$result1) 
		return -1;
	return $result3;
}


function GetOrderHistory($idaccount) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idaccount	= urldecode($idaccount);
	
	$str1 = "SELECT		o.*
			 FROM		`order` o
			 WHERE		o.`id_account`='$idaccount'	AND
						o.`is_delete`<>1
			 ORDER BY	o.`date`	DESC";
	$result1 = mysql_query($str1);
	if (mysql_num_rows($result1)<=0) {
		mysql_close($conn);
		return '[]';
	}
	$orders = array();
	while ($o = mysql_fetch_assoc($result1))
		$orders[] = $o;
	
	$result = array();
	foreach($orders as $order) {
		$idorder = $order['id'];
		$str = "SELECT		od.*,
							cl.`nameEN`	AS 	`name_color_en`, 
							cl.`nameKR`	AS 	`name_color_kr`,
							cl.`nameVN`	AS 	`name_color_vn`,
							sz.`size` 	AS 	`name_size`, 
							pd.`image` 	AS 	`product_image`, 
							pd.`name` 	AS 	`product_name`,
							cg.`nameEN`	AS	`category_name_en`,
							cg.`nameKR`	AS	`category_name_kr`,
							cg.`nameVN`	AS	`category_name_vn`
				FROM		`order_detail` od
				INNER JOIN	`product` pd
				ON			pd.`id`=od.`id_product`
				LEFT JOIN	`color` cl
				ON			cl.`id`=od.`color`
				LEFT JOIN	`size` sz
				ON			sz.`id`=od.`size`
				INNER JOIN	`category` cg
				ON			cg.`id`=pd.`id_category`
				WHERE		od.`id_order`=$idorder;";
		$result2 = mysql_query($str);
		$orderds = array();
		while ($od = mysql_fetch_assoc($result2))
			$orderds[] = $od;
		$result3 = array();
		$result3['order'] = $order;
		$result3['list_detail'] = $orderds;
		$result[] = $result3;
	}
	mysql_close($conn);
	
	return json_encode($result);
}


function GetStyler($idaccount, $start, $count) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idaccount	= urldecode($idaccount);
	$start		= urldecode($start);
	$count		= urldecode($count);
	
	$str = "SELECT		p.*,	IFNULL((	SELECT	SUM(l.`like`)
											FROM	`like` l
											WHERE	l.`id_account`='$idaccount' AND l.`id_product`=p.`id` AND l.`type`=2
										), 0) AS `liked`
			FROM		`post` p
			WHERE		p.`status`=1
			ORDER BY	p.`create_day` 	DESC
			LIMIT		$start, $count;";
				
	$result1 = mysql_query($str);
	$result2 = array();
	while ($r = mysql_fetch_assoc($result1))
		$result2[] = $r;
	
	mysql_close($conn);
	return json_encode($result2);
}


function GetMyStyle($idaccount, $start, $count) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idaccount	= urldecode($idaccount);
	$start		= urldecode($start);
	$count		= urldecode($count);
	
	$str = "SELECT		p.*,	IFNULL((	SELECT	SUM(l.`like`)
											FROM	`like` l
											WHERE	l.`id_account`='$idaccount' AND l.`id_product`=p.`id` AND l.`type`=2
										), 0) AS `liked`
			FROM		`post` p
			WHERE		p.`id_account`='$idaccount' AND p.`status`=1
			ORDER BY	p.`create_day` 	DESC
			LIMIT		$start, $count;";
			
	$result1 = mysql_query($str);
	$result2 = array();
	while ($r = mysql_fetch_assoc($result1))
		$result2[] = $r;
	
	mysql_close($conn);
	return json_encode($result2);
}


function NewProfile($idaccount, $profile) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idaccount	= urldecode($idaccount);
	$profile	= urldecode($profile);
	
	$str = "UPDATE	`account` a
			SET		a.`profile`='$profile'
			WHERE	a.`id`='$idaccount'";
			
	$result1 = mysql_query($str);
	mysql_close($conn);
	
	if ($result1)
		return 1;
	return 0;
}

function ChangePassword($idaccount, $pwd) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idaccount	= urldecode($idaccount);
	$pwd		= urldecode($pwd);
	
	$str = "UPDATE	`account`
			SET		`password`='$pwd'
			WHERE	`id`='$idaccount'";
			
	$result1 = mysql_query($str);
	mysql_close($conn);
	
	if ($result1)
		return 1;
	return 0;
}


function GetProductColor($idproduct) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idproduct	= urldecode($idproduct);
	
	$str = "SELECT 		pc.*, c.*
			FROM		`product_color` pc
			INNER JOIN	`color` c
			ON			c.`id`=pc.`id_color`
			WHERE		pc.`id_product`='$idproduct';";
			
	$result1 = mysql_query($str);
	$result2 = array();
	while ($row = mysql_fetch_assoc($result1))
		$result2[] = $row;
	
	mysql_close($conn);
	return json_encode($result2);
}


function GetProductSize($idproduct) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idproduct	= urldecode($idproduct);
	
	$str = "SELECT 		pc.*, s.*
			FROM		`product_size` pc
			INNER JOIN	`size` s
			ON			s.`id`=pc.`id_size`
			WHERE		pc.`id_product`='$idproduct';";
			
	$result1 = mysql_query($str);
	$result2 = array();
	while ($row = mysql_fetch_assoc($result1))
		$result2[] = $row;
	
	mysql_close($conn);
	return json_encode($result2);
}


function GetCategory() {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "SELECT		c.*, 
						t.`name` 	AS `tm_name`, 
						t.`image` 	AS `tm_image`,
						(	SELECT	COUNT(*)
							FROM	`product` p
							WHERE	p.`id_category`=c.`id`	AND
									p.`status`=1			AND
									p.`quantity`=1)	AS `num_product`,
						(	SELECT	COUNT(*)
							FROM	`product` p1
							WHERE	p1.`id_category`=c.`id`	AND
									p1.`status`=1			AND
									p1.`quantity`=1			AND
									p1.`fashion`=1) AS `male_fashion`,
						(	SELECT	COUNT(*)
							FROM	`product` p2
							WHERE	p2.`id_category`=c.`id`	AND
									p2.`status`=1			AND
									p2.`quantity`=1			AND
									p2.`fashion`=0) AS `female_fashion`
			FROM		`category` c
			LEFT JOIN	`trademarks` t
			ON			t.`id`=c.`id_trademarks`
			WHERE		c.`status`=1
			ORDER BY	c.`pos`		ASC,
						c.`pos1`	ASC;";
			
	$result1 = mysql_query($str);
	$result2 = array();
	
	while ($row = mysql_fetch_assoc($result1))
		$result2[] = $row;
		
	mysql_close($conn);
	return json_encode($result2);
}



function GetProductCategory($idcategory, $idaccount) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idcategory = urldecode($idcategory);
	$idaccount = urldecode($idaccount);
	
	$str = "SELECT		p.*,	IFNULL((	SELECT	SUM(l.`like`)
											FROM	`like` l
											WHERE	l.`id_account`='$idaccount' AND 
													l.`id_product`=p.`id` 		AND 
													l.`type`=1
										), 0) AS `liked`,
								c.`nameEN`	AS	`category_name_en`,
								c.`nameKR`	AS	`category_name_kr`,
								c.`nameVN`	AS	`category_name_vn`,
								c.`color`	AS	`color`,
								p.`priceVN`	AS	`price_vn`
			FROM		`product` p
			INNER JOIN	`category` c
			ON			c.`id`=p.`id_category`
			WHERE			p.`status`=1
						AND	p.`id_category`='$idcategory'
						AND	p.`quantity`=1
			ORDER BY	c.`pos`			ASC,
						c.`pos1`		ASC,
						p.`create_day`	DESC,
						p.`likes`		DESC;";
			
	$result1 = mysql_query($str);
	$result2 = array();
	
	while ($row = mysql_fetch_assoc($result1)) {
		// $row['price_vn'] = (string) (round( ((float) $row['price_vn']) / 10000) * 10000);
		// $result2[] = $row;
		$result2[] = calculateSaleOff($row);
	}
		
	mysql_close($conn);
	return json_encode($result2);
}


function GetProductCategoryByGender($idcategory, $idaccount, $gender) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idcategory = urldecode($idcategory);
	$idaccount = urldecode($idaccount);
	
	$str = "SELECT		p.*,	IFNULL((	SELECT	SUM(l.`like`)
											FROM	`like` l
											WHERE	l.`id_account`='$idaccount' AND 
													l.`id_product`=p.`id` 		AND 
													l.`type`=1
										), 0) AS `liked`,
								c.`nameEN`	AS	`category_name_en`,
								c.`nameKR`	AS	`category_name_kr`,
								c.`nameVN`	AS	`category_name_vn`,
								c.`color`	AS	`color`,
								p.`priceVN`	AS	`price_vn`
			FROM		`product` p
			INNER JOIN	`category` c
			ON			c.`id`=p.`id_category`
			WHERE			p.`status`=1
						AND	p.`id_category`='$idcategory'
						AND	p.`quantity`=1"
						. (($gender == 1 || $gender == 0) ? " AND p.`fashion`=$gender" : "")
						. "
			ORDER BY	c.`pos`			ASC,
						c.`pos1`		ASC,
						p.`create_day`	DESC,
						p.`likes`		DESC;";
			
	$result1 = mysql_query($str);
	$result2 = array();
	
	while ($row = mysql_fetch_assoc($result1)) {
		$result2[] = calculateSaleOff($row);
	}
		
	mysql_close($conn);
	return json_encode($result2);
}


function NewStyle($idaccount, $title, $image, $content) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idaccount 	= urldecode($idaccount);
	$title 		= urldecode($title);
	$image 		= urldecode($image);
	$content 	= urldecode($content);
	$ip 		= $_SERVER['REMOTE_ADDR'];
	
	$date = date("Y-m-d H:i:s");
	$str = "INSERT INTO post	(`id_account`, `date`, `title`, `image`, `content`, `create_day`, `status`, `likes`, `comments`, `ip`)
			VALUES				('$idaccount', '$date', '$title', '$image', '$content', '$date', 1, 0, 0, '$ip');";
			
	$result1 = mysql_query($str);
	$result2 = mysql_insert_id();
	
	mysql_close($conn);
	if (!$result1)
		return -1;
	return $result2;
}


function GetInfoMemberByID($idaccount, $myid) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idaccount 	= urldecode($idaccount);
	$myid		= urldecode($myid);
	
	$sql = "SELECT		a.*, IFNULL((	SELECT	COUNT(*)
											FROM	`follower` f
											WHERE	f.`id_follow`=a.`id` AND
													f.`id_mem`=$myid
										), 0) AS `followed`,
						(	SELECT 		COUNT(*)
							FROM		`follower` f1
							INNER JOIN	`account` a1
							ON			f1.`id_mem`=a1.`id`
							WHERE		f1.`id_follow`=$idaccount) AS `follow_count`						
			FROM 		`account` a
			WHERE  		`id`=$idaccount;";
	
	$result1 = mysql_query($sql);
	$result2 = new stdClass();
	if (mysql_num_rows($result1)>0)
		$result2 = mysql_fetch_assoc($result1);
		
	mysql_close($conn);
	return json_encode($result2);
}


function Follow($idmem, $idfriend) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idmem		= urldecode($idmem);
	$idfriend	= urldecode($idfriend);
	
	$date = date("Y-m-d H:i:s");
	$str = "INSERT INTO `follower`	(`id_mem`, `id_follow`, `type`, `status`, `create_date`)
			VALUES					($idmem, $idfriend, 1, 1, '$date');";
									
	$result = mysql_query($str);
	mysql_close($conn);
	
	if (!$result)
		return 0;
	return 1;
}


function UnFollow($idmem, $idfriend) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idmem		= urldecode($idmem);
	$idfriend	= urldecode($idfriend);
	
	$str = "DELETE FROM `follower`
			WHERE		`id_mem`=$idmem AND `id_follow`=$idfriend;";
					
	$result = mysql_query($str);
	mysql_close($conn);
	
	if (!$result)
		return 0;
	return 1;
}

function DeleteFollower($idmem, $idfollower) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idmem		= urldecode($idmem);
	$idfollower	= urldecode($idfollower);
	
	$str = "DELETE FROM `follower`
			WHERE		`id_mem`=$idfollower AND `id_follow`=$idmem;";
					
	$result = mysql_query($str);
	mysql_close($conn);
	
	if (!$result)
		return 0;
	return 1;
}


function GetFollower($idaccount) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idaccount	= urldecode($idaccount);
	
	$str = "SELECT		a.*, IFNULL((	SELECT	COUNT(*)
											FROM	`follower` f1
											WHERE	f1.`id_follow`=a.`id` AND
													f1.`id_mem`=$idaccount
										), 0) AS `followed`
			FROM		`account` a, `follower` f
			WHERE		f.`id_follow`=$idaccount	AND
						a.`id`=f.`id_mem`
			ORDER BY	a.`name`	ASC;";
	
	$result1 = mysql_query($str);
	$result2 = array();
	
	while ($row = mysql_fetch_assoc($result1))
		$result2[] = $row;
	
	mysql_close($conn);
	return json_encode($result2);
}


function GetFollowing($idaccount) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idaccount	= urldecode($idaccount);
	
	$str = "SELECT		a.*, IFNULL((	SELECT	COUNT(*)
											FROM	`follower` f1
											WHERE	f1.`id_follow`=a.`id` AND
													f1.`id_mem`=$idaccount
										), 0) AS `followed`
			FROM		`account` a, `follower` f
			WHERE		f.`id_mem`=$idaccount	AND
						a.`id`=f.`id_follow`
			ORDER BY	a.`name`	ASC;";
	
	$result1 = mysql_query($str);
	$result2 = array();
	
	while ($row = mysql_fetch_assoc($result1))
		$result2[] = $row;
	
	mysql_close($conn);
	return json_encode($result2);
}


function GetFollowStyle($idaccount, $start, $count) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idaccount	= urldecode($idaccount);
	$start		= urldecode($start);
	$count		= urldecode($count);
	
	$str =	"SELECT		p.*,	IFNULL((	SELECT	SUM(l.`like`)
											FROM	`like` l
											WHERE	l.`id_account`='$idaccount' AND l.`id_product`=p.`id` AND l.`type`=2
										), 0) AS `liked`
			FROM		`post` p
			WHERE		p.`status`=1	AND
						p.`id_account`	IN	(
												SELECT	f.`id_follow`
												FROM	`follower` f
												WHERE	f.`id_mem`=$idaccount
											)
			ORDER BY	p.`create_day` 	DESC
			LIMIT		$start, $count;";
			
	$result1 = mysql_query($str);
	$result2 = array();
	
	while ($row = mysql_fetch_assoc($result1))
		$result2[] = $row;
	
	mysql_close($conn);
	return json_encode($result2);
}


function GetListCity() {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "SELECT		*
			FROM		`city` c
			ORDER BY	c.`pos`	ASC";
			
	$result1 = mysql_query($str);
	$result2 = array();
	
	while ($row = mysql_fetch_assoc($result1))
		$result2[] = $row;
		
	mysql_close($conn);
	return json_encode($result2);
}


function GetListDistrict() {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "SELECT		*
			FROM		`district` d
			ORDER BY	d.`pos`	ASC";
			
	$result1 = mysql_query($str);
	$result2 = array();
	
	while ($row = mysql_fetch_assoc($result1)) 
		$result2[] = $row;
		
	mysql_close($conn);
	return json_encode($result2);
}

function GetListFavoriteProduct($idaccount, $start, $count) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idaccount 	= urldecode($idaccount);
	$start 		= urldecode($start);
	$count 		= urldecode($count);
	
	$str = "SELECT		p.*,	(SELECT 1) AS `liked`,
								c.`nameEN`	AS	`category_name_en`,
								c.`nameKR`	AS	`category_name_kr`,
								c.`nameVN`	AS	`category_name_vn`,
								c.`color`	AS	`color`,
								p.`priceVN`	AS	`price_vn`
			FROM		`product` p
			INNER JOIN	`category` c
			ON			c.`id`=p.`id_category`
			INNER JOIN	`like` l1
			ON			l1.`id_product`=p.`id`	AND
						l1.`type`=1				AND
						l1.`id_account`=$idaccount
			WHERE		p.`status`=1 AND
						p.`quantity`=1
			HAVING		`liked`>0
			ORDER BY	l1.`datetime`	DESC,
						p.`likes`		DESC,
						p.`create_day`	DESC
			LIMIT		$start, $count;";
			
	$result1 = mysql_query($str);
	$result2 = array();
	
	while ($row = mysql_fetch_assoc($result1)) {
		// $row['price_vn'] = (string) (round( ((float) $row['price_vn']) / 10000) * 10000);
		// $result2[] = $row;
		$result2[] = calculateSaleOff($row);
	}

	mysql_close($conn);
	return json_encode($result2);
}



function EditProfile($idaccount, $name, $image, $profile) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idaccount 	= urldecode($idaccount);
	$image 		= urldecode($image);
	$name 		= urldecode($name);
	$profile	= urldecode($profile);
	
	$str = "UPDATE	`account` a
			SET		a.`nick_name`='$name',
					a.`image`='$image',
					a.`profile`='$profile'
			WHERE	a.`id`=$idaccount;";
			
	$result1 = mysql_query($str);
	mysql_close($conn);
	
	if (!$result1)
		return 0;
	return 1;
}


function DeleteOrder($idorder) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idorder 	= urldecode($idorder);
	
	$str = "UPDATE	`order`
			SET		`is_delete`=1,
					`status`=-1
			WHERE	`id`=$idorder;";
						
	$result1 = mysql_query($str);
	$result2 = mysql_affected_rows();
	mysql_close($conn);
	
	if (!$result1)
		return -1;
	return $result2;
}


function GetChildCategories($idcategory) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idcategory = urldecode($idcategory);
	
	$str = "SELECT		c.*, t.`name` AS `tm_name`, t.`image` AS `tm_image`
			FROM		`category` c
			LEFT JOIN	`trademarks` t
			ON			t.`id`=c.`id_trademarks`
			WHERE		c.`id_parent`=$idcategory;";
						
	$result1 = mysql_query($str);
	$result2 = array();
	
	while ($r = mysql_fetch_assoc($result1))
		$result2[] = $r;
	
	mysql_close($conn);
	return json_encode($result2);
}


function DeleteOrder1($idorder) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idorder = urldecode($idorder);

	$str = "UPDATE	`order`
			SET		`is_delete`=1
			WHERE	`id`=$idorder;";
					
	$rs1 = mysql_query($str);
	$rs2 = mysql_affected_rows();
	mysql_close($conn);
	
	if (!$rs1)
		return -1;
	return $rs2;
}


function DeletePost($idpost) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idpost = urldecode($idpost);

	$str = "UPDATE	`post`
			SET		`status`=2
			WHERE	`id`=$idpost;";
					
	$rs1 = mysql_query($str);
	$rs2 = mysql_affected_rows();
	mysql_close($conn);
	
	return $rs2;
}


function GetProductByID($idproduct, $idaccount) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "SELECT		p.*, 	IFNULL((	SELECT	SUM(l.`like`)
											FROM	`like` l
											WHERE	l.`id_account`='$idaccount' AND l.`id_product`=p.`id` AND l.`type`=1
										), 0) AS `liked`,
								c.`nameEN`	AS	`category_name_en`,
								c.`nameKR`	AS	`category_name_kr`,
								c.`nameVN`	AS	`category_name_vn`,
								c.`color`	AS	`color`,
								p.`priceVN`	AS	`price_vn`
			FROM		`product` p
			INNER JOIN	`category` c
			ON			c.`id`=p.`id_category`
			WHERE  		p.`id`=$idproduct;";
	
	$result1 = mysql_query($str);
	$result2 = new stdClass();
	if (mysql_num_rows($result1)>0)
		$result2 = mysql_fetch_assoc($result1);
		
	mysql_close($conn);
	// $result2['price_vn'] = (string) (round( ((float) $result2['price_vn']) / 10000) * 10000);
	$result2 = calculateSaleOff($result2);
	return json_encode($result2);
}


function GetStyleByID($idstyle, $idaccount) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "SELECT		p.*,	IFNULL((	SELECT	SUM(l.`like`)
											FROM	`like` l
											WHERE	l.`id_account`='$idaccount' AND l.`id_product`=p.`id` AND l.`type`=2
										), 0) AS `liked`
			FROM		`post` p
			WHERE		p.`id`=$idstyle;";
			
	$result1 = mysql_query($str);
	$result2 = new stdClass();
	if (mysql_num_rows($result1)>0)
		$result2 = mysql_fetch_assoc($result1);
		
	mysql_close($conn);
	return json_encode($result2);
}



function ProducClicked($idproduct) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$ip = $_SERVER['REMOTE_ADDR'];
	$date = date("Y-m-d H:i:s");
	$str = "INSERT INTO	`product_view`	(`id_product`, `datetime`, `ip`)
			VALUES						($idproduct, '$date', '$ip');";
	mysql_query($str);
	
	mysql_close($conn);
	return 1;
}



function GetProductRecommended($idproduct, $idaccount) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "SELECT		p.*,
						(p.`price` 	*	(SELECT	`name` 
										 FROM 	`rate` 
										 LIMIT 	1) 
									+ 	c.`commission`) AS `price_vn`,	
						IFNULL((	SELECT	SUM(l.`like`)
									FROM	`like` l
									WHERE	l.`id_account`='$idaccount' AND 
											l.`id_product`=p.`id` AND 
											l.`type`=1
								), 0) AS `liked`,
						c.`nameEN`	AS	`category_name_en`,
						c.`nameKR`	AS	`category_name_kr`,
						c.`nameVN`	AS	`category_name_vn`,
						c.`color`	AS	`color`,
						c.`status`	AS	`st_parent`
			FROM		`recommend` c1
			INNER JOIN	`product` p
			ON			p.`id`=c1.`id_recommend`
			INNER JOIN	`category` c
			ON			c.`id`=p.`id_category`
			WHERE		c1.`id_product`=$idproduct	AND
						p.`status`=1				AND
						p.`quantity`=1
			HAVING		`st_parent`=1
			ORDER BY	c.`pos`			ASC,
						c.`pos1`		ASC,
						p.`create_day`	DESC,
						p.`likes`		DESC;";
						
	$rs1 = mysql_query($str);
	$rs2 = array();
	
	while ($r = mysql_fetch_assoc($rs1))
		$rs2[] = $r;
		
	mysql_close($conn);
	return json_encode($rs2);
}



function ForgotPassword($email) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$email = urldecode($email);
	
	$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	$length = 6;
	
	$pwd1 = substr(str_shuffle($chars), 0, $length);
	
	$salt = "orchipro";
	$pwd2 = md5($pwd1 . $salt);
	
	$str = "UPDATE	`account`
			SET		`password`='$pwd2'
			WHERE	`email`='$email';";
			
	$rs1 = mysql_query($str);
	$rs2 = mysql_affected_rows();
	
	mysql_close($conn);
	if ($rs2 < 1)
		return 0;
		
	$fields_string = "";
	$lang = "EN";
	$url = 'http://1.234.53.52/mail/shopping_forgetpass.php';
	$fields = array(
							'lang'  => urlencode($lang),
							'email' => urlencode($email),
							'pass' => urlencode($pwd1)
					);

	//url-ify the data for the POST
	foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
	rtrim($fields_string, '&');
	
	// Open connection
	$ch = curl_init();

	// Set the url, number of POST vars, POST data
	curl_setopt( $ch, CURLOPT_URL, $url );

	curl_setopt( $ch, CURLOPT_POST, true );
	// curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
	curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

	curl_setopt( $ch, CURLOPT_POSTFIELDS, $fields_string );

	curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");
	
	curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
	// Execute post
	$result = curl_exec($ch);

	//close connection
	curl_close($ch);
	
	return 1;
}



function UpdateMemberShippingInfo($idmem, $address, $city, $district, $phone) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$address = urldecode($address);
	$phone = urldecode($phone);
	
	$str = "UPDATE	`account`
			SET 	`address`='$address',
					`city`=$city,
					`district`=$district,
					`phone`='$phone'
			WHERE	`id`=$idmem;";
			
	$rs1 = mysql_query($str);
	if (!$rs1) {
		mysql_close($conn);
		return -1;
	}
	$rs2 = mysql_affected_rows();
	mysql_close($conn);
	return $rs2;
}



function GetMemberStyle($idaccount, $idmember, $start, $count) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$idaccount	= urldecode($idaccount);
	$idmember	= urldecode($idmember);
	$start		= urldecode($start);
	$count		= urldecode($count);
	
	$str = "SELECT		p.*,	IFNULL((	SELECT	SUM(l.`like`)
											FROM	`like` l
											WHERE	l.`id_account`='$idaccount' AND l.`id_product`=p.`id` AND l.`type`=2
										), 0) AS `liked`
			FROM		`post` p
			WHERE		p.`id_account`='$idmember' AND p.`status`=1
			ORDER BY	p.`create_day` 	DESC
			LIMIT		$start, $count;";
			
	$result1 = mysql_query($str);
	$result2 = array();
	while ($r = mysql_fetch_assoc($result1))
		$result2[] = $r;
	
	mysql_close($conn);
	return json_encode($result2);
}



function NewDevideID($uid, $gcmid) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$uid = urldecode($uid);
	$gcmid = urldecode($gcmid);
	
	$str = "INSERT INTO	`device`	(`device_id`, `gcm_id`)
			VALUES					('$uid', '$gcmid')
			ON DUPLICATE KEY UPDATE `gcm_id`='$gcmid';";
			
	$rs1 = mysql_query($str);
	mysql_close($conn);
	
	return ($rs1) ? 1 : 0;
}


function AppOpenTrack($iddevice) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$iddevice = urldecode($iddevice);
	$now = date("Y-m-d H:i:s");
	
	$str = "UPDATE	`device`
			SET		`launch_count`=`launch_count`+1,
					`last_try`='$now'
			WHERE	`device_id`='$iddevice';";
			
	$rs1 = mysql_query($str);
	mysql_close($conn);
	
	return ($rs1) ? 1 : 0;
}


function AppOpenTrack1($iddevice, $appver) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$iddevice = urldecode($iddevice);
	$now = date("Y-m-d H:i:s");
	
	$str = "UPDATE	`device`
			SET		`launch_count`=`launch_count`+1,
					`version`='$appver',
					`last_try`='$now'
			WHERE	`device_id`='$iddevice';";
			
	$rs1 = mysql_query($str);
	mysql_close($conn);
	
	return ($rs1) ? 1 : 0;
}



function AppOpenTrack2($iddevice, $appver, $api, $user, $day) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$user = urldecode($user);
	$day = urldecode($day);
	$iddevice = urldecode($iddevice);
	$now = date("Y-m-d H:i:s");
	
	$str = "UPDATE	`device`
			SET		`launch_count`=`launch_count`+1,
					`version`='$appver',
					`api`=$api"
					. (($user != "") ? ", `last_user`='$user'" : "") 
					. ",
					`last_try`='$now'
			WHERE	`device_id`='$iddevice';";
			
	$rs1 = mysql_query($str);
	mysql_close($conn);
	
	return ($rs1) ? 1 : 0;
}









/************************************************************
*					NOTIFICATION SERVICES					*
************************************************************/
function UpdateGCMID($idmem, $gcmid) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "UPDATE	`account`
			SET		`gcm_id`='$gcmid'
			WHERE	`id`=$idmem;";
			
	$rs1 = mysql_query($str);
	$rs2 = ($rs1) ? 1 : 0;
	mysql_close($conn);
	
	return ($rs2);
}


function SendNotification($message, $registrationIDs) {
	global $api_key;
	
	// Set POST variables
	$url = 'https://android.googleapis.com/gcm/send';
	
	$fields = array(
					'registration_ids'  => $registrationIDs,
					'data'              => array( "message" => $message ),
					);

	$headers = array( 
					'Authorization: key=' . $api_key,
					'Content-Type: application/json'
					);

	// Open connection
	$ch = curl_init();

	// Set the url, number of POST vars, POST data
	curl_setopt( $ch, CURLOPT_URL, $url );

	curl_setopt( $ch, CURLOPT_POST, true );
	curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
	curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

	curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

	curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");
	curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
	// Execute post
	$result = curl_exec($ch);

	// Close connection
	curl_close($ch);

	return $result;
}


function NotifyComment($idcomment, $cmtype) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "";
	// if ($cmtype==1) {
		// $str = "SELECT		c1.`content`,
							// a.`id`			AS	`id_mem`,
							// a.`gcm_id`,
							// a1.`id`			AS	`id_sender`,
							// a1.`name`		AS	`uid_sender`,
							// a1.`nick_name`	AS	`name_sender`,
							// c1.`id_product`	AS	`id_owner`,
							// p.`name`		AS	`uid_owner`,
							// p.`name`		AS	`name_owner`,
							// a1.`image`,
							// c1.`id_product`	AS	`id_post`,
							// c1.`date`
				// FROM		`comment` c
				// INNER JOIN	`comment` c1
				// ON			c1.`id_product`=c.`id_product`
				// INNER JOIN	`account` a
				// ON			a.`id`=c.`id_account`
				// INNER JOIN	`account` a1
				// ON			a1.`id`=c1.`id_account`
				// INNER JOIN	`product` p
				// ON			p.`id`=c1.`id_product`
				// WHERE		c1.`id`=$idcomment
				// GROUP BY	a.`id`
				// HAVING		a.`id`<>a1.`id`;";
	// }
	// if ($cmtype==2) {
		// $str = "SELECT		c1.`content`,
							// a.`id`			AS	`id_mem`,
							// a.`gcm_id`,
							// a1.`id`			AS	`id_sender`,
							// a1.`name`		AS	`uid_sender`,
							// a1.`nick_name`	AS	`name_sender`,
							// a1.`image`,
							// a2.`id`			AS	`id_owner`,
							// a2.`name`		AS	`uid_owner`,
							// a2.`nick_name`	AS	`name_owner`,
							// c1.`id_product`	AS	`id_post`,
							// c1.`date`
				// FROM		`comment` c
				// INNER JOIN	`comment` c1
				// ON			c1.`id_product`=c.`id_product`
				// INNER JOIN	`account` a1
				// ON			a1.`id`=c1.`id_account`
				// INNER JOIN	`post` p
				// ON			p.`id`=c1.`id_product`
				// INNER JOIN	`account` a2
				// ON			a2.`id`=p.`id_account`
				// INNER JOIN	`account` a
				// ON			(a.`id`=c.`id_account`)
				// WHERE		c1.`id`=$idcomment
				// GROUP BY	a.`id`
				// HAVING		a.`id`<>a1.`id`	AND
							// a.`id`<>a2.`id`;";
	// }
	
	// $rs1 = mysql_query($str);
	// if (mysql_num_rows($rs1)>0) {
		// while ($rs2 = mysql_fetch_assoc($rs1)) {
			// $id_mem = $rs2['id_mem'];
			// $id_sender = $rs2['id_sender'];
			// $uid_sender = $rs2['uid_sender'];
			// $name_sender = $rs2['name_sender'];
			// $id_owner = $rs2['id_owner'];
			// $uid_owner = $rs2['uid_owner'];
			// $name_owner = $rs2['name_owner'];
			// $content = $rs2['content'];
			// $id_post = $rs2['id_post'];
			// $image = $rs2['image'];
			// $type = 2;
			// $unread = 1;
			// $date = $rs2['date'];
			// $status = 1;
			
			// $str = "INSERT INTO	`notify`	(`id_mem`, `id_sender`, `uid_sender`, `name_sender`, `id_owner`, `uid_owner`, `name_owner`, `content`, `id_post`, `image`, `type`, `cm_type`, `unread`, `date`, `status`)
					// VALUES					('$id_mem', '$id_sender', '$uid_sender', '$name_sender', '$id_owner', '$uid_owner', '$name_owner', '$content', '$id_post', '$image', '$type', '$cmtype', '$unread', '$date', '$status');";
					
			// $rs3 = mysql_query($str);
			// if ($rs3==TRUE) {
				// $ids = array();
				// $ids[] = $rs2['gcm_id'];
				// $id1 = mysql_insert_id();
				// $notify = new Notify($id1, $id_mem, $id_sender, $uid_sender, $name_sender, $id_owner, $uid_owner, $name_owner, $content, $id_post, $image, $type, $cmtype, $unread, $date, $status);
				// $mess = json_encode($notify);
				// SendNotification($mess, $ids);
			// }
		// }
	// }
	if ($cmtype==1) {
		$str = "SELECT		c.`content`,
							a.`id`			AS	`id_mem`,
							a.`gcm_id`,
							a1.`id`			AS	`id_sender`,
							a1.`name`		AS	`uid_sender`,
							a1.`nick_name`	AS	`name_sender`,
							a1.`image`,
							p.`id_category`	AS	`id_owner`,
							p.`name`		AS	`uid_owner`,
							p.`name`		AS	`name_owner`,
							c1.`id_product`	AS	`id_post`,
							c.`date`
				FROM		`comment` c
				INNER JOIN	`product` p
				ON			p.`id`=c.`id_product`
				INNER JOIN	`comment` c1
				ON			c1.`id_product`=c.`id_product`	AND
							c1.`status`=1
				INNER JOIN	`account` a
				ON			a.`id`=c1.`id_account`	OR
							a.`id`=12
				INNER JOIN	`account` a1
				ON			a1.`id`=c.`id_account`
				WHERE		c.`id`=$idcomment
				GROUP BY	a.`id`
				HAVING		a.`id`<>a1.`id`;";
		
		$rs4 = mysql_query($str);
		if (mysql_num_rows($rs4)>0) {
			while ($rs5 = mysql_fetch_assoc($rs4)) {
				$id_mem = $rs5['id_mem'];
				$id_sender = $rs5['id_sender'];
				$uid_sender = $rs5['uid_sender'];
				$name_sender = $rs5['name_sender'];
				$id_owner = $rs5['id_owner'];
				$uid_owner = $rs5['uid_owner'];
				$name_owner = $rs5['name_owner'];
				$content = $rs5['content'];
				$id_post = $rs5['id_post'];
				$image = $rs5['image'];
				$type = 5;
				$unread = 1;
				$date = $rs5['date'];
				$status = 1;
				
				$str = "INSERT INTO	`notify`	(`id_mem`, `id_sender`, `uid_sender`, `name_sender`, `id_owner`, `uid_owner`, `name_owner`, `content`, `id_post`, `image`, `type`, `cm_type`, `unread`, `date`, `status`)
						VALUES					('$id_mem', '$id_sender', '$uid_sender', '$name_sender', '$id_owner', '$uid_owner', '$name_owner', '$content', '$id_post', '$image', '$type', '$cmtype', '$unread', '$date', '$status');";
						
				$rs6 = mysql_query($str);
				if ($rs6==TRUE) {
					$ids = array();
					$ids[] = $rs5['gcm_id'];
					$id1 = mysql_insert_id();
					$notify = new Notify($id1, $id_mem, $id_sender, $uid_sender, $name_sender, $id_owner, $uid_owner, $name_owner, $content, $id_post, $image, $type, $cmtype, $unread, $date, $status);
					$mess = json_encode($notify);
					SendNotification($mess, $ids);
				}
			}
		}
	}
	
	if ($cmtype==2) {
		$str = "SELECT		c1.`content`,
							a.`id`			AS	`id_mem`,
							a.`gcm_id`,
							a1.`id`			AS	`id_sender`,
							a1.`name`		AS	`uid_sender`,
							a1.`nick_name`	AS	`name_sender`,
							a1.`image`,
							a2.`id`			AS	`id_owner`,
							a2.`name`		AS	`uid_owner`,
							a2.`nick_name`	AS	`name_owner`,
							c1.`id_product`	AS	`id_post`,
							c.`date`
				FROM		`comment` c
				INNER JOIN	`comment` c1
				ON			c1.`id_product`=c.`id_product`
				INNER JOIN	`account` a1
				ON			a1.`id`=c1.`id_account`
				INNER JOIN	`post` p
				ON			p.`id`=c1.`id_product`
				INNER JOIN	`account` a2
				ON			a2.`id`=p.`id_account`
				INNER JOIN	`account` a
				ON			(a.`id`=p.`id_account`)
				WHERE		c1.`id`=$idcomment
				GROUP BY	a.`id`
				HAVING		a.`id`<>a1.`id`;";
				
		$rs4 = mysql_query($str);
		if (mysql_num_rows($rs4)>0) {
			$rs5 = mysql_fetch_assoc($rs4);
			$id_mem = $rs5['id_mem'];
			$id_sender = $rs5['id_sender'];
			$uid_sender = $rs5['uid_sender'];
			$name_sender = $rs5['name_sender'];
			$id_owner = $rs5['id_owner'];
			$uid_owner = $rs5['uid_owner'];
			$name_owner = $rs5['name_owner'];
			$content = $rs5['content'];
			$id_post = $rs5['id_post'];
			$image = $rs5['image'];
			$type = 1;
			$unread = 1;
			$date = $rs5['date'];
			$status = 1;
			
			$str = "INSERT INTO	`notify`	(`id_mem`, `id_sender`, `uid_sender`, `name_sender`, `id_owner`, `uid_owner`, `name_owner`, `content`, `id_post`, `image`, `type`, `cm_type`, `unread`, `date`, `status`)
					VALUES					('$id_mem', '$id_sender', '$uid_sender', '$name_sender', '$id_owner', '$uid_owner', '$name_owner', '$content', '$id_post', '$image', '$type', '$cmtype', '$unread', '$date', '$status');";
					
			$rs6 = mysql_query($str);
			if ($rs6==TRUE) {
				$ids = array();
				$ids[] = $rs5['gcm_id'];
				$id1 = mysql_insert_id();
				$notify = new Notify($id1, $id_mem, $id_sender, $uid_sender, $name_sender, $id_owner, $uid_owner, $name_owner, $content, $id_post, $image, $type, $cmtype, $unread, $date, $status);
				$mess = json_encode($notify);
				SendNotification($mess, $ids);
			}
		}
		
		$str = "SELECT		c.`content`,
							a.`id`			AS	`id_mem`,
							a.`gcm_id`,
							a1.`id`			AS	`id_sender`,
							a1.`name`		AS	`uid_sender`,
							a1.`nick_name`	AS	`name_sender`,
							a1.`image`,
							a2.`id`			AS	`id_owner`,
							a2.`name`		AS	`uid_owner`,
							a2.`nick_name`	AS	`name_owner`,
							c1.`id_product`	AS	`id_post`,
							c1.`date`
				FROM		`comment` c
				INNER JOIN	`post` p
				ON			p.`id`=c.`id_product`
				INNER JOIN	`comment` c1
				ON			c1.`id_product`=c.`id_product`	AND
							c1.`id_account`<>p.`id_account`	AND
							c1.`status`=1
				INNER JOIN	`account` a
				ON			a.`id`=c1.`id_account`
				INNER JOIN	`account` a1
				ON			a1.`id`=c.`id_account`
				INNER JOIN	`account` a2
				ON			a2.`id`=p.`id_account`
				WHERE		c.`id`=$idcomment
				GROUP BY	a.`id`
				HAVING		a.`id`<>a1.`id`;";
		
		$rs4 = mysql_query($str);
		if (mysql_num_rows($rs4)>0) {
			while ($rs5 = mysql_fetch_assoc($rs4)) {
				$id_mem = $rs5['id_mem'];
				$id_sender = $rs5['id_sender'];
				$uid_sender = $rs5['uid_sender'];
				$name_sender = $rs5['name_sender'];
				$id_owner = $rs5['id_owner'];
				$uid_owner = $rs5['uid_owner'];
				$name_owner = $rs5['name_owner'];
				$content = $rs5['content'];
				$id_post = $rs5['id_post'];
				$image = $rs5['image'];
				$type = 2;
				$unread = 1;
				$date = $rs5['date'];
				$status = 1;
				
				$str = "INSERT INTO	`notify`	(`id_mem`, `id_sender`, `uid_sender`, `name_sender`, `id_owner`, `uid_owner`, `name_owner`, `content`, `id_post`, `image`, `type`, `cm_type`, `unread`, `date`, `status`)
						VALUES					('$id_mem', '$id_sender', '$uid_sender', '$name_sender', '$id_owner', '$uid_owner', '$name_owner', '$content', '$id_post', '$image', '$type', '$cmtype', '$unread', '$date', '$status');";
						
				$rs6 = mysql_query($str);
				if ($rs6==TRUE) {
					$ids = array();
					$ids[] = $rs5['gcm_id'];
					$id1 = mysql_insert_id();
					$notify = new Notify($id1, $id_mem, $id_sender, $uid_sender, $name_sender, $id_owner, $uid_owner, $name_owner, $content, $id_post, $image, $type, $cmtype, $unread, $date, $status);
					$mess = json_encode($notify);
					SendNotification($mess, $ids);
				}
			}
		}
	}
	
	mysql_close($conn);
	return 1;
}


function NotifyFriendPost($idpost) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "SELECT		a.`id`			AS	`id_mem`,
						a.`gcm_id`,
						a1.`id`			AS	`id_sender`,
						a1.`name`		AS	`uid_sender`,
						a1.`nick_name`	AS	`name_sender`,
						a1.`image`	AS	`image`,
						a1.`id`			AS	`id_owner`,
						a1.`name`		AS	`uid_owner`,
						a1.`nick_name`	AS	`name_owner`,
						p.`create_day`	AS	`date`
			FROM		`post` p
			INNER JOIN 	`follower` f
			ON			f.`id_follow`=p.`id_account`
			INNER JOIN 	`account` a
			ON			a.`id`=f.`id_mem`
			INNER JOIN 	`account` a1
			ON			a1.`id`=p.`id_account`
			WHERE		p.`id`=$idpost
			GROUP BY	a.`id`
			HAVING		a.`id`<>a1.`id`;";
			
	$rs1 = mysql_query($str);
	if (mysql_num_rows($rs1)>0) {
		while ($r = mysql_fetch_assoc($rs1)) {
			$id_mem = $r['id_mem'];
			$id_sender = $r['id_sender'];
			$uid_sender = $r['uid_sender'];
			$name_sender = $r['name_sender'];
			$id_owner = $r['id_owner'];
			$uid_owner = $r['uid_owner'];
			$name_owner = $r['name_owner'];
			$content = "";
			$id_post = $idpost;
			$image = $r['image'];
			$type = 3;
			$unread = 1;
			$date = $r['date'];
			$status = 1;
			
			$str = "INSERT INTO	`notify`	(`id_mem`, `id_sender`, `uid_sender`, `name_sender`, `id_owner`, `uid_owner`, `name_owner`, `content`, `id_post`, `image`, `type`, `cm_type`, `unread`, `date`, `status`)
					VALUES					('$id_mem', '$id_sender', '$uid_sender', '$name_sender', '$id_owner', '$uid_owner', '$name_owner', '$content', '$id_post', '$image', '$type', '0', '$unread', '$date', '$status');";
					
			$rs2 = mysql_query($str);
			if ($rs2==TRUE) {
				$ids = array();
				$ids[] = $r['gcm_id'];
				$id1 = mysql_insert_id();
				$notify = new Notify($id1, $id_mem, $id_sender, $uid_sender, $name_sender, $id_owner, $uid_owner, $name_owner, $content, $id_post, $image, $type, $cmtype, $unread, $date, $status);
				$mess = json_encode($notify);
				SendNotification($mess, $ids);
			}
		}
	}
	
	mysql_close($conn);
	return 1;
}

function NotifyFriendAdd($idmem, $idfriend) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "SELECT		a.`id`			AS	`id_mem`,
						a.`gcm_id`,
						a1.`id`			AS	`id_sender`,
						a1.`name`		AS	`uid_sender`,
						a1.`nick_name`	AS	`name_sender`,
						a1.`image`		AS	`image`,
						a1.`id`			AS	`id_owner`,
						a1.`name`		AS	`uid_owner`,
						a1.`nick_name`	AS	`name_owner`,
						f.`create_date`	AS	`date`
			FROM		(	SELECT	*
							FROM	`follower`
							WHERE	`id_mem`=$idmem	AND
									`id_follow`=$idfriend	)	f
			INNER JOIN	`account` a
			ON 			a.`id`=f.`id_follow`
			INNER JOIN	`account` a1
			ON			a1.`id`=f.`id_mem`
			GROUP BY	a.`id`
			HAVING		a.`id`<>a1.`id`;";
	
	$rs1 = mysql_query($str);
	if (mysql_num_rows($rs1)>0) {
		$r = mysql_fetch_assoc($rs1);
		$id_mem = $r['id_mem'];
		$id_sender = $r['id_sender'];
		$uid_sender = $r['uid_sender'];
		$name_sender = $r['name_sender'];
		$id_owner = $r['id_owner'];
		$uid_owner = $r['uid_owner'];
		$name_owner = $r['name_owner'];
		$content = "";
		$id_post = 0;
		$image = $r['image'];
		$type = 4;
		$unread = 1;
		$date = $r['date'];
		$status = 1;
		
		$str = "INSERT INTO	`notify`	(`id_mem`, `id_sender`, `uid_sender`, `name_sender`, `id_owner`, `uid_owner`, `name_owner`, `content`, `id_post`, `image`, `type`, `cm_type`, `unread`, `date`, `status`)
				VALUES					('$id_mem', '$id_sender', '$uid_sender', '$name_sender', '$id_owner', '$uid_owner', '$name_owner', '$content', '$id_post', '$image', '$type', '0', '$unread', '$date', '$status');";
				
		$rs2 = mysql_query($str);
		if ($rs2==TRUE) {
			$ids = array();
			$ids[] = $r['gcm_id'];
			$id1 = mysql_insert_id();
			$notify = new Notify($id1, $id_mem, $id_sender, $uid_sender, $name_sender, $id_owner, $uid_owner, $name_owner, $content, $id_post, $image, $type, $cmtype, $unread, $date, $status);
			$mess = json_encode($notify);
			SendNotification($mess, $ids);
		}
	}
	
	mysql_close($conn);
	return 1;
}



function AddNotice($id_product, $idaccount) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$sql="SELECT c.id_account from `comment` c WHERE c.id_product='$id_product' GROUP BY c.id_account";
	$result=mysql_query($sql);
	while ($row=mysql_fetch_array($result)){
		if($idaccount!=$row['id_account'])
			mysql_query("INSERT INTO `notice` (`id_account`, `id_product`, `datetime`, `id_writer`) VALUES ('".$row['id_account']."', '$id_product', NOW(), '$idaccount')");
	}
	mysql_close($conn);
	return 1;
}



function GetNotifications($idaccount, $start, $count) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "SELECT		n.`id`,
						n.`id_mem`,
						n.`id_sender`,
						m1.`name`		AS	`uid_sender`,
						m1.`nick_name`	AS	`name_sender`,
						n.`id_owner`,
						IF (n.`cm_type`=1, p.`name`, m2.`name`)			AS	`uid_owner`,
						IF (n.`cm_type`=1, p.`name`, m2.`nick_name`)	AS	`name_owner`,
						n.`content`,
						n.`id_post`,
						m1.`image`,
						n.`type`,
						n.`cm_type`,
						n.`unread`,
						n.`date`,
						n.`status`
			FROM		`notify` n
			INNER JOIN	`account` m1
			ON			m1.`id`=n.`id_sender`
			LEFT JOIN	`account` m2
			ON			m2.`id`=n.`id_owner`
			LEFT JOIN	`product` p
			ON			p.`id`=n.`id_post`
			WHERE		n.`id_mem`=$idaccount
			ORDER BY	n.`date`	DESC
			LIMIT		$start, $count;";
	$rs1 = mysql_query($str);
	$rs2 = array();
	
	while ($r = mysql_fetch_assoc($rs1))
		$rs2[] = $r;
		
	mysql_close($conn);
	return json_encode($rs2);
}


function ReadNotification($idnoti) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "UPDATE	`notify`
			SET		`unread`=0
			WHERE	`id`=$idnoti;";
			
	$rs1 = mysql_query($str);
	$rs2 = mysql_affected_rows();
	mysql_close($conn);
	
	return $rs2;
}


function SearchProduct($pname, $idaccount) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$pname = urldecode($pname);
	
	$str = "SELECT		p.*,	IFNULL((	SELECT	SUM(l.`like`)
											FROM	`like` l
											WHERE	l.`id_account`=$idaccount AND 
													l.`id_product`=p.`id` 		AND 
													l.`type`=1
										), 0) AS `liked`,
								c.`nameEN`	AS	`category_name_en`,
								c.`nameKR`	AS	`category_name_kr`,
								c.`nameVN`	AS	`category_name_vn`,
								c.`color`	AS	`color`,
								p.`priceVN`	AS	`price_vn`
			FROM		`product` p
			INNER JOIN	`category` c
			ON			c.`id`=p.`id_category`
			WHERE			p.`status`=1
						AND	p.`name` LIKE '%$pname%'
						AND	p.`quantity`=1
			ORDER BY	p.`name`;";
	
	$rs1 = mysql_query($str);
	$rs2 = array();
	
	while ($r = mysql_fetch_assoc($rs1))
		$rs2[] = $r;
	
	mysql_close($conn);
	return json_encode($rs2);
}






/****************************************************************************
*							TEST USING $_GET METHOD							*
*																			*
****************************************************************************/
function TestFunction() {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	// $dbo = new PDO("mysql:host=$hostname;dbname=$databasename", "$username", "$password");
	// $dbo->exec("SET NAMES 'utf8'");
	// var_dump($dbo);
	// $result9 = "";
	// try {
		// $str1 = "SELECT	`name`
				 // FROM	`rate`
				 // WHERE	`id`=4;";
		// $result7 = $dbo->query($str1);
		// $result8 = $result7->fetchAll(PDO::FETCH_ASSOC);
		// $result9 = $result8[0]['name'];
	// } catch (PDOException $ex) {
		// $dbo = null;
		// echo $ex->getMessage();
		// return;
	// }
	// $dbo = null;
	$str1 = "SELECT	*
				 FROM	`rate`;";
	$rs2 = mysql_query($str1);
	$rs1 = mysql_num_rows($rs2);
	echo $rs1;
}


if(isset($_REQUEST['action'])){
	if($_REQUEST['action']=="CheckEmail" ){
		$email=$_REQUEST['email'];
		echo CheckEmail($email);
	}
	elseif($_REQUEST['action']=="CheckLogin" ){
		$email=$_REQUEST['email'];
		$pword=$_REQUEST['password'];
		echo CheckLogin($email,$pword);
	}
	elseif($_REQUEST['action']=="Signup" ){
		$name=$_REQUEST['name'];
		$email=$_REQUEST['email'];
		$pword=$_REQUEST['password'];
		echo Signup($name,$email,$pword);
	}
	elseif($_REQUEST['action']=="GetBestOfDay" ){
		$day=$_REQUEST['day'];
		$idaccount=$_REQUEST['account'];
		echo GetBestOfDay($day, $idaccount);
	}	
	elseif($_REQUEST['action']=="GetInfoMemberByEmail" ){
		$email=$_REQUEST['email'];
		$myid =$_REQUEST['myid'];
		echo GetInfoMemberByEmail($email, $myid);
	}
	elseif($_REQUEST['action']=="GetProductDetail" ){
		$idproduct=$_REQUEST['product'];
		echo GetProductDetail($idproduct);
	}
	elseif($_REQUEST['action']=="GetCommentOfProduct" ){
		$idproduct=$_REQUEST['product'];
		$type=$_REQUEST['type'];
		echo GetCommentOfProduct($idproduct, $type);
	}
	elseif($_REQUEST['action']=="NewComment" ){
		$idproduct=$_REQUEST['product'];
		$idaccount=$_REQUEST['account'];
		$content  =$_REQUEST['content'];
		$type     =$_REQUEST['type'];
		echo NewComment($idproduct, $idaccount, $content, $type);
	}
	elseif($_REQUEST['action']=="NewLike" ){
		$idproduct=$_REQUEST['product'];
		$idaccount=$_REQUEST['account'];
		$liked    =$_REQUEST['liked'];
		$type     =$_REQUEST['type'];
		echo NewLike($idproduct, $idaccount, $liked, $type);
	}
	elseif($_REQUEST['action']=="NewOrder" ){
		$idaccount	=$_REQUEST['account'];
		$address  	=$_REQUEST['address'];
		$city     	=$_REQUEST['city'];
		$state    	=$_REQUEST['state'];
		$phone    	=$_REQUEST['phone'];
		$payment  	=$_REQUEST['payment'];
		$shipping 	=$_REQUEST['ship'];
		$shippingfee=$_REQUEST['shippingfee'];
		$memo		=$_REQUEST['memo'];
		$listdetail =$_REQUEST['detail'];
		echo NewOrder($idaccount, $address, $city, $state, $phone, $payment, $shipping, $shippingfee, $memo, $listdetail);
	}
	elseif($_REQUEST['action']=="NewOrderDetail" ){
		$order    =$_REQUEST['order'];
		$product  =$_REQUEST['product'];
		$color    =$_REQUEST['color'];
		$price    =$_REQUEST['price'];
		$size     =$_REQUEST['size'];
		$quantity =$_REQUEST['quantity'];
		echo NewOrderDetail($order, $product, $color, $size, $price, $quantity);
	}
	elseif($_REQUEST['action']=="GetOrderHistory" ){
		$idaccount=$_REQUEST['account'];
		echo GetOrderHistory($idaccount);
	}
	elseif($_REQUEST['action']=="GetStyler" ){
		$idaccount=$_REQUEST['account'];
		$start=$_REQUEST['start'];
		$count=$_REQUEST['count'];
		echo GetStyler($idaccount, $start, $count);
	}
	elseif($_REQUEST['action']=="GetMyStyle" ){
		$idaccount=$_REQUEST['account'];
		$start=$_REQUEST['start'];
		$count=$_REQUEST['count'];
		echo GetMyStyle($idaccount, $start, $count);
	}
	elseif($_REQUEST['action']=="NewProfile" ){
		$idaccount=$_REQUEST['account'];
		$profile  =$_REQUEST['profile'];
		echo NewProfile($idaccount, $profile);
	}
	elseif($_REQUEST['action']=="ChangePassword" ){
		$idaccount=$_REQUEST['account'];
		$pwd =$_REQUEST['password'];
		echo ChangePassword($idaccount, $pwd);
	}
	elseif($_REQUEST['action']=="GetProductColor" ){
		$idproduct=$_REQUEST['product'];
		echo GetProductColor($idproduct);
	}
	elseif($_REQUEST['action']=="GetProductSize" ){
		$idproduct=$_REQUEST['product'];
		echo GetProductSize($idproduct);
	}
	elseif($_REQUEST['action']=="GetCategory" ){
		echo GetCategory();
	}
	elseif($_REQUEST['action']=="GetProductCategory" ){
		$idaccount=$_REQUEST['account'];
		$idcategory=$_REQUEST['category'];
		echo GetProductCategory($idcategory, $idaccount);
	}
	elseif($_REQUEST['action']=="NewStyle" ){
		$idaccount=$_REQUEST['account'];
		$title	=$_REQUEST['title'];
		$image	=$_REQUEST['image'];
		$content=$_REQUEST['content'];
		echo NewStyle($idaccount, $title, $image, $content);
	}
	elseif($_REQUEST['action']=="GetInfoMemberByID" ){
		$idaccount=$_REQUEST['account'];
		$myid	  =$_REQUEST['myid'];
		echo GetInfoMemberByID($idaccount, $myid);
	}
	elseif($_REQUEST['action']=="GetClientIPAddress" ){
		echo $_SERVER['REMOTE_ADDR'];
	}
	elseif($_REQUEST['action']=="Test" ){
		echo TestFunction();
	}
	elseif($_REQUEST['action']=="GetListDistrict" ){
		echo GetListDistrict();
	}
	elseif($_REQUEST['action']=="GetListProvince" ){
		echo GetListProvince();
	}
	elseif($_REQUEST['action']=="GetListFavoriteProduct" ){
		$idaccount	=$_REQUEST['account'];
		$start		=$_REQUEST['start'];
		$count		=$_REQUEST['count'];
		echo GetListFavoriteProduct($idaccount, $start, $count);
	}
	elseif($_REQUEST['action']=="EditProfile" ){
		$idaccount	=$_REQUEST['account'];
		$name		=$_REQUEST['name'];
		$image		=$_REQUEST['image'];
		$profile	=$_REQUEST['profile'];
		echo EditProfile($idaccount, $name, $image, $profile);
	}
	elseif($_REQUEST['action']=="GetFollowStyle" ){
		$idaccount	=$_REQUEST['account'];
		$start		=$_REQUEST['start'];
		$count		=$_REQUEST['count'];
		echo GetFollowStyle($idaccount, $start, $count);
	}
	elseif($_REQUEST['action']=="Follow" ){
		$idaccount	=$_REQUEST['account'];
		$follow		=$_REQUEST['follow'];
		echo Follow($idaccount, $follow);
	}
	elseif($_REQUEST['action']=="UnFollow" ){
		$idaccount	=$_REQUEST['account'];
		$follow		=$_REQUEST['follow'];
		echo UnFollow($idaccount, $follow);
	}
	elseif($_REQUEST['action']=="DeleteFollower" ){
		$idaccount	=$_REQUEST['account'];
		$follow		=$_REQUEST['follow'];
		echo DeleteFollower($idaccount, $follow);
	}
	elseif($_REQUEST['action']=="GetFollower" ){
		$idaccount	=$_REQUEST['account'];
		echo GetFollower($idaccount);
	}
	elseif($_REQUEST['action']=="GetFollowing" ){
		$idaccount	=$_REQUEST['account'];
		echo GetFollowing($idaccount);
	}
	elseif($_REQUEST['action']=="GetListCity" ){
		echo GetListCity();
	}
	elseif($_REQUEST['action']=="DeleteOrder" ){
		$idorder=$_REQUEST['idorder'];
		echo DeleteOrder($idorder);
	}
	elseif($_REQUEST['action']=="GetChildCategories" ){
		$idcategory=$_REQUEST['category'];
		echo GetChildCategories($idcategory);
	}
	elseif($_REQUEST['action']=="DeleteOrder1" ){
		$idorder=$_REQUEST['order'];
		echo DeleteOrder1($idorder);
	}
	elseif($_REQUEST['action']=="DeletePost" ){
		$idpost=$_REQUEST['post'];
		echo DeletePost($idpost);
	}
	elseif($_REQUEST['action']=="GetProductByID" ){
		$idmem=$_REQUEST['product'];
		$gcmid=$_REQUEST['account'];
		echo GetProductByID($idmem, $gcmid);
	}
	elseif($_REQUEST['action']=="GetStyleByID" ){
		$idmem=$_REQUEST['style'];
		$gcmid=$_REQUEST['account'];
		echo GetStyleByID($idmem, $gcmid);
	}
	elseif($_REQUEST['action']=="UpdateGCMID" ){
		$idmem=$_REQUEST['mem'];
		$gcmid=$_REQUEST['gcm'];
		echo UpdateGCMID($idmem, $gcmid);
	}
	elseif($_REQUEST['action']=="NotifyComment" ){
		$idmem=$_REQUEST['comment'];
		$gcmid=$_REQUEST['type'];
		NotifyComment($idmem, $gcmid);
	}
	elseif($_REQUEST['action']=="ProducClicked" ){
		$idproduct=$_REQUEST['product'];
		echo ProducClicked($idproduct);
	}
	elseif($_REQUEST['action']=="GetProductRecommended" ){
		$idproduct=$_REQUEST['product'];
		$idaccount=$_REQUEST['account'];
		echo GetProductRecommended($idproduct, $idaccount);
	}
	elseif($_REQUEST['action']=="ForgotPassword" ){
		$email=$_REQUEST['email'];
		echo ForgotPassword($email);
	}
	elseif($_REQUEST['action']=="UpdateMemberShippingInfo" ){
		$idmem		=$_REQUEST['idmem'];
		$address	=$_REQUEST['address'];
		$city		=$_REQUEST['city'];
		$district	=$_REQUEST['district'];
		$phone		=$_REQUEST['phone'];
		echo UpdateMemberShippingInfo($idmem, $address, $city, $district, $phone);
	}
	elseif($_REQUEST['action']=="GetMemberStyle" ){
		$idmem		=$_REQUEST['account'];
		$address	=$_REQUEST['member'];
		$city		=$_REQUEST['start'];
		$district	=$_REQUEST['count'];
		echo GetMemberStyle($idmem, $address, $city, $district);
	}
	elseif($_REQUEST['action']=="NewDevideID" ){
		$idmem		=$_REQUEST['account'];
		$address	=$_REQUEST['member'];
		echo NewDevideID($idmem, $address);
	}
	elseif($_REQUEST['action']=="AppOpenTrack" ){
		$iddevice		=$_REQUEST['iddevice'];
		echo AppOpenTrack($iddevice);
	}
	elseif($_REQUEST['action']=="CheckPDO" ){
		if (!defined('PDO::ATTR_DRIVER_NAME')) {
			echo 'PDO unavailable';
		} elseif (defined('PDO::ATTR_DRIVER_NAME')) {
			echo 'PDO available';
		}
	}
	elseif($_REQUEST['action']=="NotifyFriendPost" ){
		$idpost		=$_REQUEST['idpost'];
		NotifyFriendPost($idpost);
	}
	elseif($_REQUEST['action']=="NotifyFriendAdd" ){
		$idmem		=$_REQUEST['idmem'];
		$idfollow	=$_REQUEST['follow'];
		NotifyFriendAdd($idmem, $idfollow);
	}
	elseif($_REQUEST['action']=="GetNotifications" ){
		$idmem		=$_REQUEST['idmem'];
		$start		=$_REQUEST['start'];
		$count		=$_REQUEST['count'];
		echo GetNotifications($idmem, $start, $count);
	}
	elseif($_REQUEST['action']=="ReadNotification" ){
		$idmem		=$_REQUEST['idmem'];
		echo ReadNotification($idmem);
	}
	elseif($_REQUEST['action']=="SendNotification" ){
		echo SendNotification("", "");
	}
	elseif($_REQUEST['action']=="SearchProduct" ){
		$idmem		=$_REQUEST['name'];
		$start		=$_REQUEST['account'];
		echo SearchProduct($idmem, $start);
	}
}


function microtime_float() {
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}


function calculateSaleOff($r) {
	if ($r['sale_start'] != null && $r['sale_end'] != null && $r['sale_start'] != '0000-00-00' && $r['sale_end'] != '0000-00-00') {
		$now = date('Y-m-d');
		$start = date('Y-m-d', strtotime($r['sale_start']));
		$end = date('Y-m-d', strtotime($r['sale_end']));
		if ($now < $start || $now > $end)
			$r['sale_off'] = '0';
	}
	$r['price_sale'] = '0';
	$r['price_sale'] = (string) (round((((float) $r['price_vn']) - ((float) $r['price_vn']) * ((float) $r['sale_off']) / 100) / 10000) * 10000);
	$r['price_vn'] = (string) (round( ((float) $r['price_vn']) / 10000) * 10000);
	return $r;
}





/****************************************************************************
*								START WEB SERVICE							*
*																			*
****************************************************************************/

$namespace = "http://www.stylehanq.com/shopping_services";

$server = new soap_server();
// $server->configureWSDL("Shopping", $namespace);

$server->register("ChangePassword");
$server->register("NewProfile");
$server->register("GetMyStyle");
$server->register("GetOrderHistory");
$server->register("NewOrderDetail");
$server->register("NewOrder");								// Deprecated on version 0.45
$server->register("NewOrderNew");							// Deprecated on version 0.45
$server->register("NewOrder1");
$server->register("NewComment");							// Deprecated on version 0.45
$server->register("NewComment1");
$server->register("NewLike");
$server->register("GetCommentOfProduct");
$server->register("GetProductDetail");
$server->register("GetInfoMemberByEmail");
$server->register("GetInfoMemberByID");
$server->register("GetBestOfDay");
$server->register("Signup");								// Deprecated on version 0.45
$server->register("CheckLogin");
$server->register("CheckEmail");
$server->register("GetStyler");
$server->register("GetProductColor");
$server->register("GetProductSize");
$server->register("GetCategory");
$server->register("GetProductCategory");
$server->register("NewStyle");
$server->register("GetListCity");
$server->register("GetListDistrict");
$server->register("GetListFavoriteProduct");
$server->register("EditProfile");
$server->register("Follow");
$server->register("UnFollow");
$server->register("DeleteFollower");
$server->register("GetFollower");
$server->register("GetFollowing");
$server->register("GetFollowStyle");
$server->register("DeleteOrder");
$server->register("GetChildCategories");
$server->register("DeleteOrder1");
$server->register("DeletePost");
$server->register("UpdateGCMID");
$server->register("NotifyComment");
$server->register("GetProductByID");
$server->register("GetStyleByID");
$server->register("ProducClicked");
$server->register("GetProductRecommended");
$server->register("ForgotPassword");
$server->register("UpdateMemberShippingInfo");
$server->register("GetMemberStyle");
$server->register("NewDevideID");
$server->register("AppOpenTrack");							// Deprecated on version 0.44
$server->register("AppOpenTrack1");							// Deprecated on version 0.45
$server->register("AppOpenTrack2");
$server->register("Signup1");
$server->register("GetProductCategoryByGender");
$server->register("NotifyFriendPost");
$server->register("NotifyFriendAdd");
$server->register("AddNotice");
$server->register("GetNotifications");
$server->register("ReadNotification");
$server->register("SearchProduct");

if ( !isset( $HTTP_RAW_POST_DATA ) ) $HTTP_RAW_POST_DATA =file_get_contents( 'php://input' );
$server->service($HTTP_RAW_POST_DATA);